# -------------------------------------------------------------------------------
# Description:  
# Reference:
# Name:   graphNpm
# Author: wujunchao
# Date:   2021/10/2
# -------------------------------------------------------------------------------

import asyncio
from aiohttp import TCPConnector, ClientSession
import random
import pyecharts.options as opts
from pyecharts.charts import Graph

"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.apache.org/examples/editor.html?c=graph-npm
"""

# 全局调色盘
color = [
    '#c23531',
    '#2f4554',
    '#61a0a8',
    '#d48265',
    '#91c7ae',
    '#749f83',
    '#ca8622',
    '#bda29a',
    '#6e7074',
    '#546570',
    '#c4ccd3'
  ]

# 获取官方的数据
data = {"nodes":[
    {"color": color[random.randint(0, 10)], "label": "一带一路", "attributes": {}, "id": "一带一路", "size": 80},
    {"color": color[random.randint(0, 10)], "label": "乡村振兴", "attributes": {}, "id": "乡村振兴", "size": 40},
    {"color": color[random.randint(0, 10)], "label": "高质量发展", "attributes": {}, "id": "高质量发展", "size": 50},
    {"color": color[random.randint(0, 10)], "label": "经济增长", "attributes": {}, "id": "经济增长", "size": 30},
    {"color": color[random.randint(0, 10)], "label": "精准扶贫", "attributes": {}, "id": "精准扶贫", "size": 35}
],
    "edges": [
    {"sourceID": "一带一路", "attributes": {}, "targetID": "乡村振兴", "size":6},
    {"sourceID": "一带一路", "attributes": {}, "targetID": "高质量发展", "size":4},
    {"sourceID": "一带一路", "attributes": {}, "targetID": "经济增长", "size":2},
    {"sourceID": "经济增长", "attributes": {}, "targetID": "精准扶贫", "size":1},
    {"sourceID": "高质量发展", "attributes": {}, "targetID": "经济增长", "size":5},
    {"sourceID": "高质量发展", "attributes": {}, "targetID": "精准扶贫", "size":3},
    {"sourceID": "乡村振兴", "attributes": {}, "targetID": "精准扶贫", "size":2}
]}

nodes = [
    {
        # "x": node["x"],
        # "y": node["y"],
        "id": node["id"],
        "name": node["label"],
        "symbolSize": node["size"],
        "itemStyle": {"normal": {"color": node["color"]}},
    }
    for node in data["nodes"]
]

edges = [
    {
        "source": edge["sourceID"],
        "target": edge["targetID"],
        "lineStyle": {"normal": {"width": edge["size"]}}

    } for edge in data["edges"]
]


(
    Graph(init_opts=opts.InitOpts(width="1600px", height="800px"))
    .add(
        "",
        # series_name="",
        nodes=nodes,
        links=edges,
        repulsion=4000,
        linestyle_opts=opts.LineStyleOpts(curve=0.2),
        label_opts=opts.LabelOpts(is_show=True),
        # layout="none",
        # is_roam=True,
        # is_focusnode=True,
        # label_opts=opts.LabelOpts(is_show=False),
        # linestyle_opts=opts.LineStyleOpts(width=0.5, curve=0.3, opacity=0.7),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="NPM Dependencies"))
    .render("npm_dependencies.html")
)